<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <!-- Meta, title, CSS, favicons, etc. -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>新增价格条款页面 </title>

    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <!-- Font Awesome -->
    <link href="css/font-awesome.min.css" rel="stylesheet">
    <!-- NProgress -->
    <!--<link href="css/nprogress.css" rel="stylesheet">-->

    <!-- Custom Theme Style -->
    <link href="css/custom.min.css" rel="stylesheet">
</head>

<body class="nav-md">
<div class="container body">
    <div class="main_container">
        <div id="head">
        </div>

        <!-- top navigation -->

        <!-- /top navigation -->
        <!-- page content -->
        <div class="right_col" role="main">
            <div class="">
                <div class="page-title">
                    <div class="title_left">
                    </div>

                    <div class="title_right">
                        <div class="col-md-5 col-sm-5 col-xs-12 form-group pull-right top_search">

                        </div>
                    </div>
                </div>
                <div class="clearfix"></div>

                <div class="row">
                    <div class="col-md-12 col-sm-12 col-xs-12">
                        <div class="x_panel">
                            <div class="x_title">

                            </div>
                            <div class="x_content">

                                <form id="addMoneyFrom"  class="form-horizontal form-label-left" >


                                    <div class="item form-group">
                                        <label class="control-label col-md-3 col-sm-3 col-xs-12" >目的地 <span class="required">*</span>
                                        </label>
                                        <div class="col-md-6 col-sm-6 col-xs-6">
                                            <select id="destination" style="width: 100px;height: 35px;" class="form-control col-md-4 col-xs-4" >
                                                <option>未选择</option>
                                                <option>美国</option>
                                                <option>德国</option>
                                                <option>英国</option>
                                                <option>法国</option>
                                                <option>阿根廷</option>
                                                <option>瑞典</option>
                                                <option>芬兰</option>
                                                <option>澳大利亚</option>
                                                <option>泰国</option>
                                                <option>日本</option>
                                                <option>韩国</option>
                                                <option>俄罗斯</option>
                                                <option>埃及</option>
                                                <option>加拿大</option>
                                                <option>巴西</option>
                                            </select>
                                            <span  style="margin-left: 20px;line-height: 34px;display: none;color: red" id="desError"></span>
                                        </div>
                                    </div>
                                    <div class="item form-group">
                                        <label class="control-label col-md-3 col-sm-3 col-xs-12" >运输方式 <span class="required">*</span>
                                        </label>
                                        <div class="col-md-6 col-sm-6 col-xs-12">
                                            <select style="width: 100px;height: 35px;" class="form-control col-md-4 col-xs-4" id="tran">
                                                <option value="0">未选择</option>
                                                <option value="1">海运</option>
                                                <option value="2">空运</option>
                                            </select>
                                            <span id="tranError" style="margin-left: 20px;line-height: 34px;color: red;display: none"></span>
                                        </div>
                                    </div>
                                    <div class="item form-group">
                                        <label class="control-label col-md-3 col-sm-3 col-xs-12" >每KG价格 <span class="required">*</span>
                                        </label>
                                        <div class="col-md-6 col-sm-6 col-xs-12">
                                            <input type="text" style="width: 100px;" id="price" class="form-control col-md-7 col-xs-12">
                                            <span id="priceError" style="margin-left: 20px;line-height: 34px;color: red;display: none"></span>
                                        </div>
                                    </div>
                                    <div class="item form-group">
                                        <label class="control-label col-md-3 col-sm-3 col-xs-12" >燃油附加费 <span class="required">*</span>
                                        </label>
                                        <div class="col-md-6 col-sm-6 col-xs-12">
                                            <input type="text" style="width: 100px;" id="fuel"  class="form-control col-md-7 col-xs-12">
                                            <span id="fuelError" style="margin-left: 20px;line-height: 34px;color: red;display: none"></span>
                                        </div>
                                    </div>

                                    <span id="addMoneyError" style="color: red;margin-left: 400px;font-size: 20px;"></span>
                                    <div class="ln_solid"></div>
                                    <div class="form-group">
                                        <div class="col-md-6 col-md-offset-3">

                                            <button type="button" class="btn btn-primary back" onclick="javascrtpt:window.location.href = 'showmoney.html'" >返回</button>
                                            <a href="javascript:;" type="button" class="btn btn-success" id="addMoney">新增</a>
                                        </div>
                                    </div>
                                </form>

                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!-- /page content -->

        <!-- footer content -->
        <footer>
            <div class="pull-right">
                Gentelella - Bootstrap Admin Template by <a href="https://colorlib.com">Colorlib</a>
            </div>
            <div class="clearfix"></div>
        </footer>
        <!-- /footer content -->
    </div>
</div>

<!-- jQuery -->
<script src="js/jquery.min.js"></script>
<!-- Bootstrap -->
<script src="js/bootstrap.min.js"></script>
<!-- FastClick -->
<script src="js/fastclick.js"></script>
<!-- NProgress -->
<script src="js/nprogress.js"></script>
<!-- validator -->

<!-- Custom Theme Scripts -->

<!-- validator -->
<script >
    var $token=window.sessionStorage.getItem('token');



    $(document).ready(function(){
        $("#head").load("head.html");
        $("#pay").click(function  () {
            window.location.href="pay.html";
        })
    });
    // initialize the validator function
    // validator.message.date = 'not a real date';

    // validate a field on "blur" event, a 'select' on 'change' event & a '.reuired' classed multifield on 'keyup':
    // $('form')
    //     .on('blur', 'input[required], input.optional, select.required', validator.checkField)
    //     .on('change', 'select.required', validator.checkField)
    //     .on('keypress', 'input[required][pattern]', validator.keypress);

    $('.multi.required').on('keyup blur', 'input', function() {
        validator.checkField.apply($(this).siblings().last()[0]);
    });

    $('form').submit(function(e) {
        e.preventDefault();
        var submit = true;

        // evaluate the form using generic validaing
        if (!validator.checkAll($(this))) {
            submit = false;
        }

        if (submit)
            this.submit();

        return false;
    });

    $("#destination").focus(function(){
        $("#addMoneyError").hide();
    });
    $("#tran").focus(function(){
        $("#addMoneyError").hide();
    });
    $("#price").focus(function(){
        $("#addMoneyError").hide();
    });
    $("#fuel").focus(function(){
        $("#addMoneyError").hide();
    });


    $("#destination").blur(function(){
        var $destination = $("#destination").val();
        if ($destination == "未选择"){
            $("#desError").show();
            $("#desError").html("请选择目的地");
        }else {
            $("#desError").hide();
        }
    });
    $("#tran").blur(function(){
        var $tran = $("#tran").val();
        if ($tran == 0){
            $("#tranError").show();
            $("#tranError").html("请选择运输方式");
        }else {
            $("#tranError").hide();
        }
    });
    $("#price").blur(function(){
        var priPattern = /(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/;
        var $price = $("#price").val();
        if ($price == null || !(priPattern.test($price))){
            $("#priceError").show();
            $("#priceError").html("请输入正确的价格");
        }else {
            $("#priceError").hide();
        }
    });
    $("#fuel").blur(function(){
        var fuelPattern = /(^[1-9]\d*(\.\d{1,4})?$)|(^0(\.\d{1,4})?$)/;
        var $fuel = $("#fuel").val();
        if ($fuel == null || !(fuelPattern.test($fuel))){
            $("#fuelError").show();
            $("#fuelError").html("请输入正确的燃油附加费(小数最多4位)");
        }else {
            $("#fuelError").hide();
        }
    });

    $("#addMoney").click(function () {
        var fuelPattern = /(^[1-9]\d*(\.\d{1,4})?$)|(^0(\.\d{1,4})?$)/;
        var priPattern = /(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/;
        if ($("#destination").val()== "未选择"){
            $("#desError").show();
            $("#desError").html("请选择目的地");
            return;
        }else if ($("#tran").val()==0) {
            $("#tranError").show();
            $("#tranError").html("请选择运输方式");
            return;
        }else if ($("#price").val() == null || !(priPattern.test($("#price").val()))) {
            $("#priceError").show();
            $("#priceError").html("请输入正确的价格");
            return;
        }else if ($("#fuel").val() == null || !(fuelPattern.test($("#fuel").val()))) {
            $("#fuelError").show();
            $("#fuelError").html("请输入正确的燃油附加费(小数最多4位)");
            return;
        }else {
            var $destination = $("#destination").val();
            var $tran = $("#tran").val();
            var $price = $("#price").val();
            var $fuel = $("#fuel").val();

            $.ajax({
                type:"post",
                xhrFields:{withCredentials:true},
                url:"http://localhost:8081/money/addMoney",
                dataType:"json",
                data:{"Destination":$destination,"tranNo":$tran,"unitPrice":$price,"fuel":$fuel,"token":$token},
                success:function(data) {
                    if(data.message=="no"){
                        $("#addMoneyError").show();
                        $("#addMoneyError").html("请不要重复添加");
                        return;
                    }else{
                        window.location.href='showmoney.html';
                    }
                },
                error:function(xx){
                    alert("异常");

                }
            });
        }
    });
</script>
<!-- /validator -->
</body>
</html>
